import http from '@/utils/http-util'

let BASE_ROLE_CONTENT = process.env.WEB_CONTEXT_PATH + 'sys/role'
export function getRoleTreeApi() {
  return http({
    url: BASE_ROLE_CONTENT + '/tree',
    method: 'get',
    params: http.queryParam(),
    loading: false
  })
}

export function getRoleInfoApi(roleId) {
  return http({
    url: BASE_ROLE_CONTENT + '/' + roleId,
    method: 'get',
    params: http.queryParam(),
    loading: false
  })
}

export function saveRoleApi(data = {}) {
  return http({
    url: BASE_ROLE_CONTENT,
    method: 'post',
    data: http.bodyData({
      id: data.id,
      code: data.code,
      name: data.name,
      remark: data.remark,
      parentId: data.parentId || 0
    }),
    loading: false
  })
}

export function updateRoleApi(data = {}) {
  return http({
    url: BASE_ROLE_CONTENT,
    method: 'put',
    data: http.bodyData({
      id: data.id,
      name: data.name,
      remark: data.remark,
      parentId: data.parentId || 0
    }),
    loading: false
  })
}

export function deleteRoleApi(roleId, all = false) {
  return http({
    url: BASE_ROLE_CONTENT + '/' + roleId,
    method: 'delete',
    params: http.queryParam({ all }),
    loading: false
  })
}

export function getRoleBelowMenusApi(roleId, data = {}) {
  return http({
    url: BASE_ROLE_CONTENT + '/' + roleId + '/menus',
    method: 'post',
    data: http.bodyData({
      'page': data.page || 1,
      'pageSize': data.pageSize || 20,
      'sort': {
        'field': data.sortField,
        'rule': data.sortRule === undefined ? 1 : data.sortRule
      }
    })
  })
}

export function saveRoleMenuApi(roleId, data = []) {
  return http({
    url: BASE_ROLE_CONTENT + '/' + roleId + '/menu',
    method: 'post',
    data: http.bodyData({
      'menuIds': data || []
    })
  })
}

export function deleteRoleMenuApi(roleId, menuId) {
  return http({
    url: BASE_ROLE_CONTENT + '/' + roleId + '/' + menuId,
    method: 'delete',
    params: http.queryParam(),
    loading: false
  })
}

export function batchDeleteRoleMenuApi(roleId, menuIds = []) {
  return http({
    url: BASE_ROLE_CONTENT + '/' + roleId + '/menus',
    method: 'delete',
    data: http.bodyData({
      'menuIds': menuIds || []
    }),
    loading: false
  })
}

export function getRoleBelowMenuTreeApi(roleId) {
  return http({
    url: BASE_ROLE_CONTENT + '/' + roleId + '/menu/tree',
    method: 'get',
    params: http.queryParam(),
    loading: false
  })
}
